<?php

namespace app\api\controller;


use app\common\exception\ApiException;

class Category extends ApiException {
    public function index() {
        $params    = input('get.');
        $condition = [];
        if ($params['type'] == 1) {
            $condition['parent_id'] = ['eq', 0];
            $condition['status']    = config('code.status_show');
            $result                 = model('Category')->getList($condition);
            return show(config('code.success'), 'OK', $result);
        }
        if ($params['type'] == 2) {
            $condition['status'] = config('code.status_show');
            $result              = model('Category')->getList($condition);
            $arr                 = [];
            foreach ($result as $k => &$i1) {
                if ($i1->parent_id === 0) {
                    $i1['secList'] = [];
                    array_push($arr, $i1);
                }
            }
            foreach ($arr as $k => &$item) {
                $elements = $item['secList'];
                foreach ($result as $k1 => $value) {
                    if ($value['parent_id'] === $item['id']) {
                        array_push($elements, $value);
                    }
                }
                $item['secList'] = $elements;
            }
            return show(config('code.success'), 'OK', $arr);
        }
        if ($params['type'] == 3) {
            $condition['status']    = config('code.status_show');
            $condition['parent_id'] = $params['parent_id'];
            $result                 = model('Category')->getList($condition);
            return show(config('code.success'), 'OK', $result);
        }
    }
}